package com.zmj.utils;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.zmj.entity.ScheduleJob;
import com.zmj.exception.RZException;
import com.zmj.quartz.MyQuartzJob;
import org.quartz.*;

public class ScheduleUtils {

    private static  final  String JOB_NAME_PREFIX="myJob_";
    private static final  String JOB_GROUP_PREFIX="MyGroup_";
    private static final String TRIGGER_NAME_PREFIX="myTrigger_";
    private static final String TRIGGER_GROUP_PREFIX="MyTriggerGroup_";





    public static  void createTask(Scheduler scheduler, ScheduleJob scheduleJob){
        try{
            JobDetail jobDetail= JobBuilder.newJob(MyQuartzJob.class).
                    withIdentity(JOB_NAME_PREFIX+scheduleJob.getJobId(),
                    JOB_GROUP_PREFIX+scheduleJob.getJobId()).build();
            System.out.println(jobDetail.getKey());

            String json= JSON.toJSONString(scheduleJob);
            jobDetail.getJobDataMap().put("scheduleJob",json);

            CronTrigger cronTrigger= TriggerBuilder.newTrigger()
                    .withSchedule(CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression()))
                    .withIdentity(TRIGGER_NAME_PREFIX+scheduleJob.getJobId(),
                    TRIGGER_GROUP_PREFIX+scheduleJob.getJobId()).build();

            scheduler.scheduleJob(jobDetail,cronTrigger);
            scheduler.start();
        }catch (Exception e){
          // throw  new RZException("创建定时任务失败");
        }
    }
    public static void updateScheduler(Scheduler scheduler,ScheduleJob scheduleJob){
           try {
               TriggerKey triggerKey =TriggerKey.triggerKey(TRIGGER_NAME_PREFIX+scheduleJob.getJobId(),TRIGGER_GROUP_PREFIX+scheduleJob.getJobId());
               CronTrigger trigger=(CronTrigger) scheduler.getTrigger(triggerKey);

               CronTrigger newTrigger=trigger.getTriggerBuilder().withSchedule(CronScheduleBuilder.cronSchedule(scheduleJob.getCronExpression())).build();

               scheduler.rescheduleJob(triggerKey,newTrigger);
           }catch (SchedulerException e){
               //throw new RZException("修改定时任务失败！");
           }
    }
    public  static boolean deleteScheduler(Scheduler scheduler,long jobId){
        try {
            return  scheduler.deleteJob(JobKey.jobKey(JOB_NAME_PREFIX+jobId,JOB_GROUP_PREFIX+jobId));
        } catch (SchedulerException e) {
            throw  new  RZException("删除定时任务失败！");
        }
    }
    public     static void    pause(Scheduler scheduler,long jobId){
        try {
            JobKey jobKey = JobKey.jobKey(JOB_NAME_PREFIX+jobId,JOB_GROUP_PREFIX+jobId);
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
           // throw  new  RZException("暂停定时任务失败！");
        }
    }

    public     static void    resume(Scheduler scheduler,long jobId){
        try {
            JobKey jobKey = JobKey.jobKey(JOB_NAME_PREFIX+jobId,JOB_GROUP_PREFIX+jobId);
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
           // throw new RZException("恢复定时任务失败！");
        }
    }

    public     static void    runOnce(Scheduler scheduler,long jobId){
        try{
            JobKey jobKey=JobKey.jobKey(JOB_NAME_PREFIX+jobId,JOB_GROUP_PREFIX+jobId);
            scheduler.triggerJob(jobKey);
        }catch (SchedulerException e){
            //throw  new RZException("运行定时任务失败！");
        }
    }
}
